package com.cyf.plc.ui.fragment.set

import android.content.SharedPreferences
import android.os.Bundle
import android.widget.EditText
import androidx.navigation.Navigation
import androidx.preference.Preference
import androidx.preference.PreferenceFragmentCompat
import com.afollestad.materialdialogs.MaterialDialog
import com.afollestad.materialdialogs.callbacks.onPreShow
import com.afollestad.materialdialogs.customview.customView
import com.afollestad.materialdialogs.customview.getCustomView
import com.afollestad.materialdialogs.lifecycle.lifecycleOwner
import com.cyf.plc.BuildConfig
import com.cyf.plc.R
import com.cyf.plc.event.PlcInitEvent
import com.cyf.plc.plc.PlcExecutor
import com.jakewharton.processphoenix.ProcessPhoenix
import org.greenrobot.eventbus.EventBus

class SettingFragment : PreferenceFragmentCompat(), SharedPreferences.OnSharedPreferenceChangeListener {

    override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) {
        addPreferencesFromResource(R.xml.root_preferences)
        //设置plc 地址
        findPreference<Preference>("plcIp")?.setOnPreferenceClickListener {
            context?.let { it1 ->
                MaterialDialog(it1).show {
                    onPreShow { dialog ->
                        val etPlcIp: EditText = dialog.getCustomView().findViewById(R.id.et_plc_ip)
                        etPlcIp.setText(PlcExecutor.getPlcIp())
                    }
                    customView(R.layout.dialog_plc_ip)
                    positiveButton(R.string.sure) { dialog ->
                        val etPlcIp: EditText = dialog.getCustomView().findViewById(R.id.et_plc_ip)
                        PlcExecutor.savePlcIp(etPlcIp.text.toString())
                    }
                    negativeButton(android.R.string.cancel)
                    lifecycleOwner(this@SettingFragment)
                }
            }
            false
        }

        findPreference<Preference>("systemSet")?.setOnPreferenceClickListener {
            //跳转系统设置Fragment

            view?.let { it1 ->

                Navigation.findNavController(it1)
                    .navigate(R.id.action_settingFragment_to_setAttributeFragment)
            }
            false
        }

        //设置属性值列表
        findPreference<Preference>("attributeSet")?.setOnPreferenceClickListener {
            //跳转系统属性设置Fragment

            view?.let { it1 ->

                Navigation.findNavController(it1)
                    .navigate(R.id.action_settingFragment_to_setSubAttributeFragment)
            }
            false
        }

        //设置面板
        findPreference<Preference>("panelsSet")?.setOnPreferenceClickListener {
            //跳转面板设置Fragment
            view?.let { it1 ->

                Navigation.findNavController(it1)
                    .navigate(R.id.action_settingFragment_to_setPanelFragment)
            }
            false
        }
        //从云服务器拉取面板配置
        findPreference<Preference>("panelsSetByYun")?.setOnPreferenceClickListener {
            //跳转面板设置Fragment
            EventBus.getDefault().post(PlcInitEvent())
            false
        }

        //重启
        findPreference<Preference>("reboot")?.setOnPreferenceClickListener {
            //跳转系统属性设置Fragment
            ProcessPhoenix.triggerRebirth(context)
            false
        }

        //故障
        findPreference<Preference>("fault")?.setOnPreferenceClickListener {
            //跳转故障界面
            view?.let { it1 ->
                Navigation.findNavController(it1)
                    .navigate(R.id.action_settingFragment_to_plcFaultFragment)
            }
            false
        }

        //版本号
        findPreference<Preference>("about")?.setSummary("版本号:"+BuildConfig.VERSION_NAME)
    }

    override fun onSharedPreferenceChanged(sharedPreferences: SharedPreferences?, key: String?) {

    }

}